/*
 * @lc app=leetcode.cn id=203 lang=javascript
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
    const nextList = (head, val) => {
        if (!head) {
            return null
        }
        if (head.next) {
            head.next = nextList(head.next, val)
        }
        if (head.val === val) {
            return head.next
        } else {
            return head
        }
    }
    if (head && head.next) {
        head.next = nextList(head.next, val)
    }
    if (head && head.val === val) {
        head = head.next
    }
    return head

};
// @lc code=end

